#include "friendquest.h"
#include "ui_friendquest.h"

friendquest::friendquest(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::friendquest)
{
    ui->setupUi(this);

    this->setWindowIcon(QIcon(":/res/comm2"));
    this->setWindowTitle("Friend Request");

    getRequests();
}

friendquest::friendquest(int id, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::friendquest)
{
    ui->setupUi(this);
    connect(&cont, &control::allquit, [=](){
       close();
    });

    this->setWindowIcon(QIcon(":/res/comm2"));
    this->setWindowTitle("Friend Request");

    user.id = id;
    getRequests();
}

friendquest::~friendquest()
{
    delete ui;
}

// 获取好友申请表
void friendquest::getRequests()
{
    qDebug() << "正在加载好友申请表" << endl;
    tcpSocket = new QTcpSocket();
    tcpSocket->abort();
    tcpSocket->connectToHost(serverIp, serverPort);
    if(!tcpSocket->waitForConnected(3000))
    {
        // 连接失败，强制下线，重新进入登录界面
        QMessageBox::warning(this, "Warning!", "网络错误12", QMessageBox::Yes);
        this->close();
        user.islogin = false;
        exit(-1);
//        client *cli = new client();
//        cli->show();
    }
    else
    {
        // 服务器连接成功
        // getfriendquest##id

        QString pre = "getfriendquest##";
        QString req = pre + QString::number(user.id);
        int bytesWritten = tcpSocket->write(req.toUtf8());
        if (bytesWritten == -1) {
            qDebug() << "写入发生错误" << endl;
        }
        else if (bytesWritten < req.toUtf8().size()) {
            qDebug() << "没有完全写入所有数据" << endl;
        }
//        else{
//            qDebug() << "pre: " << pre << endl;
//            qDebug() << "req: " << req <<  endl;
//            qDebug() <<"已发送字节数：" << bytesWritten <<" 写入成功"<<endl;
//        }

        tcpSocket->flush();

        connect(tcpSocket, &QTcpSocket::readyRead, [=](){
           QByteArray buffer = tcpSocket->readAll();
           qDebug() << "buffer: " << buffer << endl;
           if("getfriendquest_ok" == QString(buffer).section("##", 0, 0))
           {
               int fqcnt = QString(buffer).section("##", 1, 1).toInt(); // 好友申请数
               qDebug() << "获取好友申请表成功, 好友申请：" << fqcnt << "条" << endl;
               int cur = 0;
               while(fqcnt--)
               {
                   QString fname = QString(buffer).section("##", 2 + cur, 2 + cur);
                   QListWidgetItem* lit = new QListWidgetItem(ui->listWidget);
                   friendquestItem* fqit = new friendquestItem(fname + "申请添加您为好友", this);
                   fqit->setParent(this);
                   lit->setSizeHint(fqit->size());
                   ui->listWidget->addItem(lit);
                   ui->listWidget->setItemWidget(lit, fqit);
                   connect(fqit, &friendquestItem::delItem, [=](){
                       ui->listWidget->takeItem(cur);
                       delete lit;
                   });
                   ++cur;
               }
           }
           else
           {
               qDebug() << "当前无好友申请记录..................." << endl;
               // 插入一条：当前无好友申请记录
               QListWidgetItem* it = new QListWidgetItem(ui->listWidget);
               ui->listWidget->addItem(it);
               it->setText("当前无好友申请记录");
           }
        });
    }
}
